SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the Contact table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Contact_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@FullName nvarchar (50)  = null ,

	@Email nvarchar (50)  = null ,

	@Phone nvarchar (20)  = null ,

	@Description nvarchar (500)  = null ,

	@ContactType varchar (20)  = null ,

	@CreatedDate datetime   = null ,

	@UpdatedDate datetime   = null ,

	@CreatedUser varchar (50)  = null ,

	@UpdatedUser varchar (50)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id]
	, [FullName]
	, [Email]
	, [Phone]
	, [Description]
	, [ContactType]
	, [CreatedDate]
	, [UpdatedDate]
	, [CreatedUser]
	, [UpdatedUser]
    FROM
	[dbo].[Contact]
    WHERE 
	 ([Id] = @Id OR @Id IS NULL)
	AND ([FullName] = @FullName OR @FullName IS NULL)
	AND ([Email] = @Email OR @Email IS NULL)
	AND ([Phone] = @Phone OR @Phone IS NULL)
	AND ([Description] = @Description OR @Description IS NULL)
	AND ([ContactType] = @ContactType OR @ContactType IS NULL)
	AND ([CreatedDate] = @CreatedDate OR @CreatedDate IS NULL)
	AND ([UpdatedDate] = @UpdatedDate OR @UpdatedDate IS NULL)
	AND ([CreatedUser] = @CreatedUser OR @CreatedUser IS NULL)
	AND ([UpdatedUser] = @UpdatedUser OR @UpdatedUser IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id]
	, [FullName]
	, [Email]
	, [Phone]
	, [Description]
	, [ContactType]
	, [CreatedDate]
	, [UpdatedDate]
	, [CreatedUser]
	, [UpdatedUser]
    FROM
	[dbo].[Contact]
    WHERE 
	 ([Id] = @Id AND @Id is not null)
	OR ([FullName] = @FullName AND @FullName is not null)
	OR ([Email] = @Email AND @Email is not null)
	OR ([Phone] = @Phone AND @Phone is not null)
	OR ([Description] = @Description AND @Description is not null)
	OR ([ContactType] = @ContactType AND @ContactType is not null)
	OR ([CreatedDate] = @CreatedDate AND @CreatedDate is not null)
	OR ([UpdatedDate] = @UpdatedDate AND @UpdatedDate is not null)
	OR ([CreatedUser] = @CreatedUser AND @CreatedUser is not null)
	OR ([UpdatedUser] = @UpdatedUser AND @UpdatedUser is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
